package com.cn.longdistancebusstation.httpHelper;
import com.cn.longdistancebusstation.BuildConfig;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by mal on 2018/1/21.
 */

public class HttpHelper {
    private OkHttpClient mOkHttpClient;
    private Retrofit mRetrofit;
    private HttpServiceBuilder mBuilder;
    private WebServiceBuilder mWebServiceBuilder;
    //
    private HttpHelper(HttpServiceBuilder builder){
        mBuilder = builder;
        mOkHttpClient = new OkHttpClient.Builder().build();
        mRetrofit = new Retrofit.Builder()
                .baseUrl(builder.baseURL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(mOkHttpClient)
                .build();
    }
    //
    private HttpHelper(WebServiceBuilder builder){
        mWebServiceBuilder = builder;
        mOkHttpClient = new OkHttpClient.Builder().build();
        mRetrofit = new Retrofit.Builder()
                .baseUrl(builder.baseURL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .client(mOkHttpClient)
                .build();
    }
    //
    public Retrofit getRetrofit() {
        return mRetrofit;
    }

    //
    public static final class HttpServiceBuilder{
        private String baseURL;
        public HttpHelper build(){
            baseURL = BuildConfig.SERVER_URL;
            HttpHelper httpHelper = new HttpHelper(this);
            return httpHelper;
        }
    }

    public static final class WebServiceBuilder{
        private String baseURL;
        public HttpHelper build(){
            baseURL = BuildConfig.STATION_SERVER_URL;
            HttpHelper httpHelper = new HttpHelper(this);
            return httpHelper;
        }
    }
}
